/**
 * 用户存储用户登录的信息
 */
export class StorageUtils {
  /**
     * 存储信息到缓存
     * @param key
     * @param data
     * @param encrypt
     */
  static addStorage(key: string, data: string, encrypt = false): any {
    return new Promise((resolve, reject) => {
      wx.setStorage({
        key: key, 
        data: data, 
        encrypt: encrypt,
        success() {
          resolve('');
        },
        fail(err) {
          reject(err.errMsg);
        }
      });
    })
  }
  /**
   * 从缓存中获取记录
   * @param key
   * @param encrypt
   */
  static getStorage(key: string, encrypt = false): any {
    return new Promise((resolve, reject) => {
      wx.getStorage({
        key: key, 
        encrypt: encrypt,
        success(res) {
          resolve(res.data);
        },
        fail(err) {
          console.error(err);
          resolve(null);
        }
      })
    })
  }
  /**
   * 从存储中删除数据
   * @param key 
   */
  static delStorage(key: string): any {
    return new Promise((resolve, reject) => {
      wx.removeStorage({
        key: key,
        success() {
          resolve('');
        },
        fail(err) {
          reject(err.errMsg);
        }
      })
    });
  }
  /**
   * 存储信息到缓存
   * @param key 
   * @param data 
   */
  static addStorageSync(key: string, data: string): void {
    wx.setStorageSync(key, data);
  }
  /**
   * 从缓存中获取记录
   * @param key 
   */
  static getStorageSync(key: string): string {
    return wx.getStorageSync(key);
  }

  /**
   * 从存储中删除数据
   * @param key
   */
  static delStorageSync(key: string): void {
    wx.removeStorageSync(key);
  }
}